<template>
  <!-- hidden PageHeaderWrapper title demo -->
  <page-header-wrapper :title='false'>
    <a-card :body-style="{padding: '24px 32px'}" :bordered='false'>
      <a-form @submit='handleSubmit' :form='form'>
        <a-form-item v-show='false' label='id'
                     :labelCol='{lg: {span: 7}, sm: {span: 7}}'
                     :wrapperCol='{lg: {span: 10}, sm: {span: 17} }'>
          <a-input v-decorator="['id', { initialValue: 0 }]" />
        </a-form-item>
        <a-form-item
          label='系统名称'
          :labelCol='{lg: {span: 7}, sm: {span: 7}}'
          :wrapperCol='{lg: {span: 10}, sm: {span: 17} }'>
          <a-input
            v-decorator="['sysName', {rules: [{ required: true, message: '请输入系统名称' }]}]"
            placeholder='请输入系统名称' />
        </a-form-item>
        <a-form-item
          label='公司名称'
          :labelCol='{lg: {span: 7}, sm: {span: 7}}'
          :wrapperCol='{lg: {span: 10}, sm: {span: 17} }'>
          <a-input
            v-decorator="['enterpriseName', {rules: [{ required: true, message: '请输入公司名称' }]}]"
            placeholder='请输入公司名称' />
        </a-form-item>
        <a-form-item
          label='公司LOGO'
          :labelCol='{lg: {span: 7}, sm: {span: 7}}'
          :wrapperCol='{lg: {span: 10}, sm: {span: 17} }'>
          <a-upload
            v-if='fileListFlag'
            action='https://p01.jiachengtech.com/adminapi/file/image/upload'
            list-type='picture'
            :default-file-list='fileList'
            :headers='headers'
            class='upload-list-inline'
            @change='fileChange'
          >
            <a-button>
              <a-icon type='upload' />
              上传
            </a-button>
          </a-upload>
        </a-form-item>
        <a-form-item
          :wrapperCol='{ span: 24 }'
          style='text-align: center'
        >
          <a-button htmlType='submit' type='primary'>保存</a-button>
        </a-form-item>
      </a-form>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import pick from 'lodash.pick'
import { SystemConfig, UpdateSystemConfig } from '@/api/system'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'

const fields = ['enterpriseName', 'id', 'logo', 'sysName']
export default {
  name: 'BaseForm',
  data() {
    return {
      form: this.$form.createForm(this),
      model: {
        username: this.$store.getters.userInfo.data.username,
        newPassword: '',
        oldPassword: '',
        enterpriseName: '',
        logo: ''
      },
      fileList: [],
      headers: {},
      fileListFlag: true
    }
  },
  created() {
    this.SystemConfig()
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 获取ACCESS_TOKEN
    const token = storage.get(ACCESS_TOKEN)
    console.log('token', token)
    this.headers['Authorization'] = token
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      console.log(this.model)
      this.model && this.form.setFieldsValue(pick(this.model, fields))
      // this.model.alarmsData && this.form.setFieldsValue(pick(this.model.alarmsData, fields))
    })
  },
  methods: {
    fileChange(file) {
      // let fileList = [...info.fileList];
      // if (fileList.length > 0) {
      //   return false
      // }
      console.log('t1', file)
      if (file.file.status === 'uploading') {
        return false
      }
      if (file.file.status === 'removed') {
        this.model.logo = ''
        return false
      }
      this.model.logo = file.file.response.data
      this.fileList = file.fileList
    },
    SystemConfig() {
      SystemConfig().then(res => {
        this.model = res.data
        this.fileList = []
        if (res.data.logo) {
          console.log('res.data', res.data)
          this.fileList.push({
            uid: '-1',
            name: 'logo.png',
            status: 'done',
            url: res.data.logo,
            thumbUrl: res.data.logo
          })
          console.log('this.fileList', this.fileList)
          this.fileListFlag = false
          this.$nextTick(() => {
            this.fileListFlag = true
          })
        }
      })
    },
    // handler
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('this.form.getFieldsValue()', this.form.getFieldsValue())
          const obj = this.form.getFieldsValue()
          this.model.n
          this.model.enterpriseName = obj.enterpriseName
          this.model.sysName = obj.sysName
          // this.model.logo = obj.logo
          UpdateSystemConfig(this.model).then(res => {
            this.$message.info('保存成功!')
            this.SystemConfig()
          })
        }
      })
    }
  }
}
</script>
<style scoped lang='less'>
/deep/ .upload-list-inline .ant-upload-list-item {
  float: left;
  width: 200px;
  margin-right: 8px;
}

/deep/ .upload-list-inline .ant-upload-animate-enter {
  animation-name: uploadAnimateInlineIn;
}

/deep/ .upload-list-inline .ant-upload-animate-leave {
  animation-name: uploadAnimateInlineOut;
}
</style>